package com.xinxin.leetcode.common;

/**
 * @author ：史鑫鑫
 * @date ：Created in 2019/5/7 23:20
 */
public class TreeUtil {
    private static int index;

    static {
        index = 0;
    }

    private static TreeNode create(int[] arr) {
        if (index < arr.length) {
            if (arr[index] == 0) {
                index++;
                return null;
            }
            TreeNode root = new TreeNode(arr[index]);
            index++;
            root.left = create(arr);
            root.right = create(arr);
            return root;
        }
        return null;
    }

    public static TreeNode createTree(int[] arr) {
        index = 0;
        return create(arr);
    }

    public static void preorderPrint(TreeNode root) {
        if (root != null) {
            System.out.println(root.val);
            preorderPrint(root.left);
            preorderPrint(root.right);
        }
    }

    public static void inorderPrint(TreeNode root) {
        if (root != null) {
            inorderPrint(root.left);
            System.out.println(root.val);
            inorderPrint(root.right);
        }
    }

    public static void postorderPrint(TreeNode root) {
        if (root != null) {
            postorderPrint(root.left);
            postorderPrint(root.right);
            System.out.println(root.val);
        }
    }
}
